<?php

namespace app\common\model\banxi\core;

use app\common\enums\AccountTypeEnum;
use app\common\model\common\BaseModel;
use think\model\concern\SoftDelete;

/**
 * 账户课时表
 */
class CoreClassHourRw extends BaseModel
{
    use SoftDelete;

    protected $name = 'banxi_core_class_hour_rw';

    protected $autoWriteTimestamp = true;

    protected $deleteTime = 'del';

    protected $defaultSoftDelete = 0;

    protected $append = [
        'accountTypeCn'
    ];

    protected $type = [
        'id' => 'string',
        'del' => 'string',
        'studentId' => 'string',
        'tradeType' => 'boolean',
        'createTime' => 'timestamp:Y-m-d H:i:s',
        'updateTime' => 'timestamp:Y-m-d H:i:s',
    ];

    //-----------------------------------------------------------------------------------
    //-                                   获取器                                         -
    //-----------------------------------------------------------------------------------


    public function getAccountTypeCnAttr($value, $data)
    {
        $value = $value ?: ($data['accountType'] ?? '');
        return (new AccountTypeEnum())->getDescription($value) ?? '';
    }

    //-----------------------------------------------------------------------------------
    //-                                   处理方法                                       -
    //-----------------------------------------------------------------------------------


    public function setAccountOwnedId(string $accountId, string $accountType)
    {
        $saveDate['accountId'] = $accountId;
        $saveDate['accountType'] = $accountType;
        switch ($accountType) {
            case AccountTypeEnum::ORGAN:
                $saveDate['organizationId'] = $accountId;
                break;
            case AccountTypeEnum::STUDENT:
                $saveDate['studentId'] = $accountId;
                break;
        }

        return $saveDate;
    }

    //-----------------------------------------------------------------------------------
    //｜                                   模型关联                                       ｜
    //-----------------------------------------------------------------------------------


}